package com.eshop.modules.shop.service.impl;


import java.io.IOException;
import com.eshop.utils.FileUtil;
import javax.servlet.http.HttpServletResponse;
import com.eshop.common.utils.QueryHelpPlus;

import com.eshop.modules.shop.service.dto.SystemGroupDataDto;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.shop.service.dto.SystemGroupDataQueryCriteria;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSON;
import java.util.function.Function;
import com.eshop.enums.CommonEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.shop.service.SystemGroupDataService;
import com.eshop.modules.shop.domain.SystemGroupData;
import com.eshop.modules.shop.service.mapper.SystemGroupDataMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class SystemGroupDataServiceImpl extends BaseServiceImpl<SystemGroupDataMapper, SystemGroupData> implements SystemGroupDataService
{
    private final IGenerator generator;
    
    public List<JSONObject> getDatas(final String name) {
        final List<SystemGroupData> systemGroupDatas = (List<SystemGroupData>)(this.baseMapper).selectList((Wrapper)((Wrappers.<SystemGroupData>lambdaQuery().eq(SystemGroupData::getGroupName, name)).eq(SystemGroupData::getStatus, CommonEnum.SHOW_STATUS_1.getValue())).orderByDesc(SystemGroupData::getSort));
        //只需要返回value部分，因此查出数据后取出value部分转成JSONObject即可
        final List<JSONObject> list = systemGroupDatas.stream().map(SystemGroupData::getValue).map(JSON::parseObject).collect(Collectors.toList());
        return list;
    }
    
    public Map<String, Object> queryAll(final SystemGroupDataQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<SystemGroupData> page = (PageInfo<SystemGroupData>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        final List<SystemGroupDataDto> systemGroupDataDTOS = new ArrayList<SystemGroupDataDto>();
        for (final SystemGroupData systemGroupData : page.getList()) {
            final SystemGroupDataDto systemGroupDataDTO = (SystemGroupDataDto)this.generator.convert(systemGroupData, (Class)SystemGroupDataDto.class);
            systemGroupDataDTO.setMap((Map<String, Object>)JSON.parseObject(systemGroupData.getValue()));
            systemGroupDataDTOS.add(systemGroupDataDTO);
        }
        map.put("content", systemGroupDataDTOS);
        map.put("totalElements", page.getTotal());
        return map;
    }

    //这种查询类很踏马牛逼啊，用注解直接自动生成Wrapper，快的一批
    public List<SystemGroupData> queryAll(final SystemGroupDataQueryCriteria criteria) {
        return (this.baseMapper).selectList(QueryHelpPlus.getPredicate(SystemGroupData.class, criteria));
    }
    
    public void download(final List<SystemGroupDataDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final SystemGroupDataDto yxSystemGroupData : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("对应的数据名称", yxSystemGroupData.getGroupName());
            map.put("数据组对应的数据值（json数据）", yxSystemGroupData.getValue());
            map.put("添加数据时间", yxSystemGroupData.getAddTime());
            map.put("数据排序", yxSystemGroupData.getSort());
            map.put("状态（1：开启；2：关闭；）", yxSystemGroupData.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public SystemGroupDataServiceImpl(final IGenerator generator) {
        this.generator = generator;
    }
}
